<?php
class Model_Admin_SysModel extends Model{
    public function getInfo($name,$offset,$rows){
        $total= DB::select(array(DB::expr('count(1)'), 'total'))->from('sysmodel')->where("caption",'like','%'.$name.'%')->execute()->get("total");
        $sql="select t.sysmodelid id,p.caption pcaption,t.pid,t.caption caption,t.url,t.sort,t.createdon,t.createdby,t.remark
             from sysmodel t
             left join sysmodel p on p.sysmodelid=t.pid
             where t.caption like '%$name%'
             and t.isdeleted<>'1'
             order by t.CreatedOn desc";
        $data=DB::query(Database::SELECT,$sql)->execute()->as_array();
        return array("total"=>$total,"rows"=>$data);
    }
    public  function saveInfo($id,$pid,$caption,$url,$sort,$remark){
       
        if($id==""){
            return $this->add($pid,$caption,$url,$sort,$remark);
        }
        else{
            return $this->alter($id,$pid,$caption,$url,$sort,$remark);
        }
    }
    private function add($pid,$caption,$url,$sort,$remark){
        try {
            list($insert_id, $total_rows)= DB::insert('sysmodel')->columns(array('pid','caption','url','sort','remark','CreatedBy','CreatedOn'))
                ->values(array($pid,$caption,$url,$sort,$remark,Session::instance()->get('admin_username',NULL),date("Y-m-d h:i:sa")))->execute();
            Database::instance()->commit();
            return json_encode(array("info"=>"success","count"=>$total_rows,"id"=>$insert_id));
        }
        catch (Exception $e) {
            Database::instance()->rollback();
            return json_encode(array("info"=>"服务器错误，保存失败，请联系管理员或者重试"));
        }
    }
    private function alter($id,$pid,$caption,$url,$sort,$remark){
        try {
            $total_rows=DB::update('sysmodel')->set(array('pid'=>$pid,'caption'=>$caption,'url'=>$url,'sort'=>$sort,'remark'=>$remark))
                ->where('sysmodelid','=',$id)->execute();
            Database::instance()->commit();
            return json_encode(array("info"=>"success","count"=>$total_rows));
        }
        catch (Exception $e) {
            Database::instance()->rollback();
            return json_encode(array("info"=>"服务器错误，保存失败，请联系管理员或者重试"));
        }
    }
    public function getModelByUsersid($tag,$usersid,$name,$offset,$pageSize){
        if(empty($tag)){
             $sql="select count(1) as total
             from `sysmodel` t
             where t.caption like '%$name%'
             and exists
             (select 1 from `user_model` g where g.sysmodelid=t.sysmodelid and g.usersid='$usersid')";

        }
        else{
             $sql="select count(1) as total
             from `sysmodel` t
             where t.caption like '%$name%'
             and not exists
             (select 1 from `user_model` g where g.sysmodelid=t.sysmodelid and g.usersid='$usersid')";
        }
       
        $total= DB::query(Database::SELECT,$sql)->execute()->get("total");

        if(empty($tag)){
             $sql="select t.sysmodelid id,t.caption,p.caption pcaption,g.look,g.add,g.edit,g.remove,g.user_modelid  umid
             from `sysmodel` t
             left join `sysmodel` p on p.sysmodelid=t.pid
             left join `user_model` g on g.sysmodelid=t.sysmodelid and g.usersid='$usersid'
             where t.caption like '%$name%'
             and exists
             (select 1 from `user_model` g where g.sysmodelid=t.sysmodelid and g.usersid='$usersid')
             order by t.CreatedOn desc
             limit $offset,$pageSize";
        }
        else{
            $sql="select t.sysmodelid id,t.caption,p.caption pcaption,g.look,g.add,g.edit,g.remove,g.user_modelid  umid
             from `sysmodel` t
             left join `sysmodel` p on p.sysmodelid=t.pid
             left join `user_model` g on g.sysmodelid=t.sysmodelid and g.usersid='$usersid'
             where t.caption like '%$name%'
             and not exists
             (select 1 from `user_model` g where g.sysmodelid=t.sysmodelid and g.usersid='$usersid')
             order by t.CreatedOn desc
             limit $offset,$pageSize";
        }
        
        $data=DB::query(Database::SELECT,$sql)->execute()->as_array();

        return array("total"=>$total,"rows"=>$data);
    }
    public function getUserSysModel(){

        $userid=Session::instance()->get('admin_userid');

        $sql="select um.sysmodelid,m.caption,m.pid,m.url from user_group g
            join user_model um on um.usersid=g.usersid and um.look='1'
            left join sysmodel m on um.sysmodelid=m.sysmodelid
            where userid='$userid'
            and m.isdeleted<>'1'
            order by sort desc";

        $data=DB::query(Database::SELECT,$sql)->execute()->as_array();

        return $data;

    }

    public function getUserLimit($url=''){

        $userid=Session::instance()->get('admin_userid');

        $sql="select um.sysmodelid,um.look,um.`add`,um.edit,um.remove
            from user_group g 
            left join user_model um on um.usersid=g.usersid
            left join sysmodel m on m.sysmodelid=um.sysmodelid
            where g.userid='$userid'
            and m.url='$url'";


        $data=DB::query(Database::SELECT,$sql)->execute()->as_array();

        if(!empty($data)){
            return $data[0];
        }
        else{
            return $data;
        }
    }
}